var JOB = {
		addJobControlInterview: function(){
			$('a.job-ajax-interview-edit').click(function(){
				var $ob = $(this);
				var $str = $('<div></div>');
				
				$.fancybox.close();
				$str.html('<center><img style="margin-top:30px;" src="/css/admin/img/global/ajax-loader.gif"/> &nbsp;'+$ob.attr('data-loading')+'</center>');
				$.fancybox.open($str,{
					padding : 7,
					title: $ob.attr('data-title-apply'),
					width: 800,
					helpers: {
						overlay:{
							closeClick : false
						} 
					}
				});
				$.ajax({
					type: "POST",
					url: '/admin/candidates/apply',
					data: {
						candidate_id: $ob.attr('data-candidate-id'),
						job_id: $ob.attr('data-job-id'),
						action_name: $ob.attr('data-action')
					},
					cache: false,
					success: function(data){
						$.fancybox.close();
						$.fancybox.open($str.html(data),{
							padding : 10,
							title: $ob.attr('data-title-apply'),
							width: 800,
							helpers: {
								overlay:{
									closeClick : false
								} 
							}
						});
						JOB.applyForm();
					}
				});
			});
			$('a.job-ajax-interview-delete').click(function(){
				var $ob = $(this);
				if(confirm($ob.attr('data-confirm'))){
					$.ajax({
						type: "POST",
						url: '/admin/candidates/apply',
						data: {
							id: $ob.attr('data-id'),
							action_name: $ob.attr('data-action')
						},
						cache: false,
						success: function(data){
							var js = $.parseJSON(data);
							alert(js.data);
							if(parseInt(js.status) == 1){
								$ob.parent().parent().remove();
							}
						}
					});
				}
			});
		},
		addCandidateControlInterviewPopup: function(){
			$('a.candidate-ajax-interview-edit').click(function(){
				var $ob = $(this);
				var $str = $('<div></div>');
				
				$.fancybox.close();
				$str.html('<center><img style="margin-top:30px;" src="/css/admin/img/global/ajax-loader.gif"/> &nbsp;'+$ob.attr('data-loading')+'</center>');
				$.fancybox.open($str,{
					padding : 7,
					title: $ob.attr('data-title-apply'),
					width: 800,
					helpers: {
						overlay:{
							closeClick : false
						} 
					}
				});
				$.ajax({
					type: "POST",
					url: '/admin/candidates/apply',
					data: {
						candidate_id: $ob.attr('data-candidate-id'),
						job_id: $ob.attr('data-job-id'),
						action_name: $ob.attr('data-action')
					},
					cache: false,
					success: function(data){
						$.fancybox.close();
						$.fancybox.open($str.html(data),{
							padding : 10,
							title: $ob.attr('data-title-apply'),
							width: 800,
							helpers: {
								overlay:{
									closeClick : false
								} 
							}
						});
						JOB.applyForm();
					}
				});
			});
			$('a.candidate-ajax-interview-delete').click(function(){
				var $ob = $(this);
				if(confirm($ob.attr('data-confirm'))){
					$.ajax({
						type: "POST",
						url: '/admin/candidates/apply',
						data: {
							id: $ob.attr('data-id'),
							action_name: $ob.attr('data-action')
						},
						cache: false,
						success: function(data){
							var js = $.parseJSON(data);
							alert(js.data);
							if(parseInt(js.status) == 1){
								$ob.parent().parent().remove();
							}
						}
					});
				}
			});
		},
		
		searchCandidateInJobList: function(){
			$('a.ajax-search-candidate').click(function(){
				var $ob = $(this);
				var $content = $('#candidate-searched-'+$ob.attr('data-id'));
				var $content_detail = $('div.candidate-content', $content);
				$content_detail.html('<center><img src="/css/admin/img/global/ajax-loader.gif"/> &nbsp;'+$ob.attr('data-loading')+'</center>');
				$content.show();
				$.ajax({
					type: "POST",
					url: '/admin/companies/searchCandidates',
					data: {
						id: $ob.attr('data-id'),
						action_name: $ob.attr('data-action')
					},
					cache: false,
					success: function(data){
						var dataJson = $.parseJSON(data);
						if(parseInt(dataJson.status,10)==1){
							var str = '<table border="1" class="dt-tbl-ct-border" cellpadding="2" cellspacing="0">'
								+'<tr>'
								+'<td class="dt-ct-header">'+$ob.attr('data-title-name')+'</td>'
								+'<td class="dt-ct-header" style="width:60px">'+$ob.attr('data-title-gender')+'</td>'
								+'<td class="dt-ct-header" style="width:70px">'+$ob.attr('data-title-dateofbirth')+'</td>'
								+'<td class="dt-ct-header">'+$ob.attr('data-title-major')+'</td>'
								+'<td class="dt-ct-header" style="width:60px">'+$ob.attr('data-title-salary')+'</td>'
								+'<td class="dt-ct-header-final">'+$ob.attr('data-title-location')+'</td>'
								+ '<td class="dt-ct-header" style="width:100px">'+$ob.attr('data-title-action')+'</td>'
								+'</tr>';
							if(typeof(dataJson.data.candidate) == undefined || dataJson.data.candidate == null){
								str+=+'<tr>'
								+'<td colspan="6"><center>'+dataJson.data+'</center></td>'
								+'</tr>';
							}else{
								for(var i = 0; i < dataJson.data.candidate.length; i++){
									str+='<tr>'
									+'<td><a href="#" data-loading="'+$ob.attr('data-loading')+'" data-title-apply="'+$ob.attr('data-title-apply')+'" data-title-detail="'+dataJson.data.candidate[i].full_name+'" data-action="'+$ob.attr('data-action')+'" data-job-id="'+dataJson.data.id+'" data-id="'+dataJson.data.candidate[i].id+'" class="ajax-candidate-detail" title="'+$ob.attr('data-noti-detail')+'">'+dataJson.data.candidate[i].full_name+'</a></td>'
									+'<td>'+dataJson.data.candidate[i].gender+'</td>'
									+'<td>'+dataJson.data.candidate[i].date_of_birth+'</td>'
									+'<td>'+dataJson.data.candidate[i].majors+'</td>'
									+'<td>'+dataJson.data.candidate[i].salary_require+'</td>'
									+'<td>'+dataJson.data.candidate[i].locations+'</td>'
									+'<td><a href="#" data-loading="'+$ob.attr('data-loading')+'" data-title-apply="'+$ob.attr('data-title-apply')+'" class="ajax-candidate-apply-master" data-action="'+$ob.attr('data-action')+'" data-candidate-id="'+dataJson.data.candidate[i].id+'" data-job-id="'+dataJson.data.id+'">'+$ob.attr('data-control-apply')+'</a></td>'
									+'</tr>';
								}
							}
							
							str+='</table>';
							$content_detail.html(str);
							JOB.viewCandidateDetail();
							JOB.applyCandidateInJobList();
						}else{
							$content_detail.html('<center>'+dataJson.data+'</center>');
						}
					}
				});
			});
		},
		viewCandidateDetail: function(){
			$('a.ajax-candidate-detail').click(function(){
				var $ob = $(this);
				var $str = $('<div></div>');
				$str.html('<center><img style="margin-top:30px;" src="/css/admin/img/global/ajax-loader.gif"/> &nbsp;'+$ob.attr('data-loading')+'</center>');
				$.fancybox.open($str,{
					padding : 7,
					title: $ob.attr('data-title-detail'),
					width: 800,
					helpers: {
						overlay:{
							closeClick : false
						} 
					}
				});
				
				$.ajax({
					type: "POST",
					url: '/admin/candidates/candidateDetails',
					data: {
						id: $ob.attr('data-id'),
						action_name: $ob.attr('data-action')
					},
					cache: false,
					success: function(data){
						var strControl = '<div class="dt-control">'
							+'<a href="#" class="ajax-candidate-apply" data-title-apply="'+$ob.attr('data-title-apply')+'" data-loading="'+$ob.attr('data-loading')+'" data-action="'+$ob.attr('data-action')+'" data-candidate-id="'+$ob.attr('data-id')+'" data-job-id="'+$ob.attr('data-job-id')+'">'
							+$ob.attr('data-title-apply')
							+'</a>';
							+'</div>';
						$.fancybox.close();
						$.fancybox.open($str.html(strControl).append(data).append(strControl),{
							padding : 10,
							title: $ob.attr('data-title-detail'),
							width: 800,
							helpers: {
								overlay:{
									closeClick : false
								} 
							}
						});
						JOB.applyCandidateInDetail();
					}
				});
				
				
			});
		},
		applyCandidateInDetail: function(){
			$('a.ajax-candidate-apply').click(function(){
				var $ob = $(this);
				var $str = $('<div></div>');
				
				$.fancybox.close();
				$str.html('<center><img style="margin-top:30px;" src="/css/admin/img/global/ajax-loader.gif"/> &nbsp;'+$ob.attr('data-loading')+'</center>');
				$.fancybox.open($str,{
					padding : 7,
					title: $ob.attr('data-title-apply'),
					width: 800,
					helpers: {
						overlay:{
							closeClick : false
						} 
					}
				});
				$.ajax({
					type: "POST",
					url: '/admin/candidates/apply',
					data: {
						candidate_id: $ob.attr('data-candidate-id'),
						job_id: $ob.attr('data-job-id'),
						action_name: $ob.attr('data-action')
					},
					cache: false,
					success: function(data){
						$.fancybox.close();
						$.fancybox.open($str.html(data),{
							padding : 10,
							title: $ob.attr('data-title-apply'),
							width: 800,
							helpers: {
								overlay:{
									closeClick : false
								} 
							}
						});
						JOB.applyForm();
					}
				});
			});
		},
		applyCandidateInJobList: function(){
			$('a.ajax-candidate-apply-master').click(function(){
				var $ob = $(this);
				var $str = $('<div></div>');
				
				$.fancybox.close();
				$str.html('<center><img style="margin-top:30px;" src="/css/admin/img/global/ajax-loader.gif"/> &nbsp;'+$ob.attr('data-loading')+'</center>');
				$.fancybox.open($str,{
					padding : 7,
					title: $ob.attr('data-title-apply'),
					width: 800,
					helpers: {
						overlay:{
							closeClick : false
						} 
					}
				});
				$.ajax({
					type: "POST",
					url: '/admin/candidates/apply',
					data: {
						candidate_id: $ob.attr('data-candidate-id'),
						job_id: $ob.attr('data-job-id'),
						action_name: $ob.attr('data-action')
					},
					cache: false,
					success: function(data){
						$.fancybox.close();
						$.fancybox.open($str.html(data),{
							padding : 10,
							title: $ob.attr('data-title-apply'),
							width: 800,
							helpers: {
								overlay:{
									closeClick : false
								} 
							}
						});
						JOB.applyForm();
					}
				});
			});
		},
		applyForm: function(){
			$('input.date-picker-interview').datepicker({minDate: $('#date_apply').val()});
			$('span.error').hide();
			$('input#button-submit').click(function(){
				$('span.error').hide();
				var $ob = $(this);
				var $dateInterview = $('#CandidateToJobs_date_interview');
				var $note = $('#CandidateToJobs_note_description');
				var $result = $('select#CandidateToJobs_result_status');//$('input[type=radio].result:checked');//children('option:selected').text()
				var $comment = $('#CandidateToJobs_comment_from_employer');
				var $candidate = $('#CandidateToJobs_candidate_id');
				var $job = $('#CandidateToJobs_job_id');
				
				if($dateInterview.val()==''){
					$dateInterview.next('span.error').show();
					$dateInterview.focus();
					$dateInterview.focus();
					return false;
				}
				$.ajax({
					type: "POST",
					url: '/admin/candidates/apply',
					data: {
						candidate_id: $candidate.val(),
						job_id: $job.val(),
						note_description: $note.val(),
						date_interview: $dateInterview.val(),
						comment_from_employer:  $comment.val(),
						result_status:  $result.children('option:selected').val(),
						
						action_name: $ob.attr('data-action')
					},
					cache: false,
					success: function(data){
						var js = $.parseJSON(data);
						alert(js.data);
						if(parseInt(js.status)==1){
							$.fancybox.close();
							window.location.reload();
						}
					}
				});
			});
			
		}
}